package com.business.etl.flow.process;

import com.google.common.collect.Lists;
import com.component.api.constants.FlowConstants;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 描述：
 * 结果缓存对象
 *  对线程环境下 处理麻烦
 *  这里存储数据量有限
 * @author xianggj
 * @Date 2021/12/9 15:27
 **/
public class ResultCache implements Serializable {
    private static final long serialVersionUID = -1L;

    private static final Map<String, List<String>> CACHE = new ConcurrentHashMap<>();

    public static void set(String key, List<String> list){
        CACHE.put(key, list);
    }

    public static void set(String key, String row){
        if (CACHE.containsKey(key)){
            List<String> list = CACHE.get(key);
            if (list.size() < FlowConstants.MAX_LINE){
                list.add(row);
            }
        } else {
            CACHE.put(key, Lists.newArrayList(row));
        }

    }

    public static List<String> get(String key){
        if (CACHE.containsKey(key)){
            //缓存数据 只取一次
            return CACHE.remove(key);
        }else {
            return Lists.newArrayList();
        }
    }

}
